<!--suppress ALL -->
<div class="layui-fluid layui-anim website-anim" id="website-entrust-item" lay-title="资格审查"
     xmlns:th="http://www.w3.org/1999/xhtml">
    <div class="layui-row website-container">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body website-table-full">
                    <form class="layui-form layui-table-form" lay-filter="item-table-form">
                        <div class="layui-row">
                            <div class="layui-col-md10">
                                <div class="layui-form-item">
                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-sm">课题名称</label>
                                        <div class="layui-input-inline">
                                            <input type="text" name="name" autocomplete="off" class="layui-input"
                                                   placeholder="请输入课题名称">
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-sm">主持人</label>
                                        <div class="layui-input-inline">
                                            <input type="text" name="leaderName" autocomplete="off" class="layui-input"
                                                   placeholder="请输入主持人">
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-sm">学科分类</label>
                                        <div class="layui-input-inline">
                                            <select name="subjectTypeCode">
                                                <option value="">请选择</option>
                                                <option
                                                        th:each="item : ${subjectType}"
                                                        th:value="${item.code}" th:text="${item.name}">
                                                </option>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <label class="layui-form-label">期次</label>
                                        <div class="layui-input-inline" style="width: 100px;">
                                            <input type="text" id="entrustItemYear" name="year" autocomplete="off"
                                                   class="layui-input">
                                        </div>
                                        <div class="layui-form-mid">-</div>
                                        <div class="layui-input-inline">
                                            <select name="planNo">
                                                <option value=""></option>
                                                <option
                                                        th:each="item : ${planNos}"
                                                        th:value="${item.code}" th:text="${item.name}">
                                                </option>
                                            </select>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="layui-col-md2 layui-col-sm12 layui-col-xs12 table-action-area">
                                <div class="layui-btn layui-btn-sm layui-btn-primary website-button-blue-plain table-action"
                                     id="query">
                                    查询
                                </div>
                                <div class="layui-btn layui-btn-sm layui-btn-primary website-button-green-plain table-action"
                                     id="reset">
                                    重置
                                </div>
                                <div class="layui-btn layui-btn-sm layui-btn-primary website-button-green-plain table-action"
                                     id="export">
                                    导出
                                </div>
                            </div>
                        </div>
                    </form>
                    <table lay-filter="itemTable" lay-data="{id: 'itemTable'}"></table>
                </div>
            </div>
        </div>
    </div>
</div>
<!--表格中字典的显示-->
<script type="text/html" id="subjectType">
    {{#
    var dict=layui.dict.getByCode(d.subjectTypeCode);
    }}
    {{#  if(dict){ }}
    <span>{{ dict.name }}</span>
    {{#  } }}
</script>
<script type="text/html" id="researchType">
    {{#
    var dict=layui.dict.getByCode(d.researchTypeCode);
    }}
    {{#  if(dict){ }}
    <span>{{ dict.name }}</span>
    {{#  } }}
</script>
<script type="text/html" id="achievementFormType">
    {{#
    var dictList=layui.dict.getByCodes(d.planAchievementFormTypeCode ? d.planAchievementFormTypeCode.split(','): '');
    }}
    <span>
    {{# layui.each(dictList,function(index,item) { }}
    {{ item.name }}&nbsp;
    {{#  }); }}
    </span>
</script>
<script type="text/html" id="statusTypeCode">
    {{#
    var dict=layui.dict.getByCode(d.statusTypeCode);
    }}
    {{#  if(dict){ }}
    <span>{{ dict.name }}</span>
    {{#  } }}
</script>
<script type="text/html" id="leaderCompany">
    {{#  if(d.leader){ }}
    <span>{{ d.leader.company }}</span>
    {{#  } }}
</script>

<script type="text/html" id="levelType">
    {{#
    var dict=layui.dict.getByCode(d.planLevelTypeCode);
    }}
    {{#  if(dict){ }}
    <span>{{ dict.name }}</span>
    {{#  }else { }}
    <span>未定级</span>
    {{#  } }}
</script>

<!--表格中的操作栏-->
<script type="text/html" id="item-option">
    {{# if(!d.isItemPublish && d.statusTypeCode==='conclude_passed'){}}
    <a lay-event="uploadItemFile" class="website-green">上传</a>
    {{#  } }}
    {{# if(d.itemFileId!=null && !d.isItemPublish){}}
    <a lay-event="publish" class="website-orange">发布</a>
    {{#  } }}
    {{# if(d.itemFileId!=null && d.isItemPublish){}}
    <a class="website-orange" {{="href=download/" +d.itemFileId}}>下载</a>
    {{#  } }}
    {{# if(!d.isItemPublish && d.statusTypeCode==='conclude_passed'){}}
    <a lay-event="download" class="website-orange">下载模板</a>
    {{#  } }}

</script>

<script data-th-inline="none" type="text/javascript">
    layui.use(['dropdown', 'jquery', 'form', 'table', 'website', 'dict', 'laydate'], function () {
        var $ = layui.jquery,
            website = layui.website,
            form = layui.form,
            table = layui.table,
            dict = layui.dict,
            laydate = layui.laydate,
            dropdown = layui.dropdown,
            $view = $('#website-entrust-item'),
            $query = $view.find('#query'),
            $reset = $view.find('#reset'),
            $export = $view.find('#export'),
            $searchForm = $view.find('form'),
            sortObject = {field: 'createTime', type: null},
            tableIns,
            createTimeFrom,
            createTimeTo;
        form.render();
        initTable();
        //初始化期次的搜索框
        laydate.render({
            elem: '#entrustItemYear', //指定元素
            trigger: 'click',
            type: 'year'
        });
        /**
         * table操作栏的按钮监听事件
         **/
        table.on('tool(itemTable)', function (obj) {
            var data = obj.data,
                layEvent = obj.event;
            if (layEvent === 'uploadItemFile') {
                website.modal.open('上传结项通知书', 'biz/item/uploadItemFile/' + data.declareId + "/" + data.topicPlan.planId, {
                    area: $(window).width() <= 750 ? '90%' : '50%',
                    offset: '30px',
                    btn: ['提交', '取消'],
                    yes: function (index, layero) {
                        $('#itemFile-update').find('#submit').trigger('click');

                    },
                    btn2: function () {
                        layer.closeAll();
                    }
                });
            }

            if (layEvent === 'publish') {
                website.modal.confirm('发布', '发布后不能再进行任何修改，确定要发布吗？', function () {
                    website.post(ctx + 'biz/item/entrust/publish/' + data.declareId, null, function () {
                        layer.closeAll();
                        website.alert.success('发布成功！');
                        $('#website-entrust-item').find('#query').click();
                    });
                });
            }
            if (layEvent === 'download') {
                website.download(ctx + 'exportCertificate/' + data.declareId, null, data.name + "结项证书.doc");
            }
        });

        /**
         * 表格的排序点击
         **/
        table.on('sort(itemTable)', function (obj) {
            sortObject = obj;
            tableIns.reload({
                initSort: obj,
                where: $.extend(getQueryParams(), {
                    field: obj.field,
                    order: obj.type
                })
            });
        });

        $query.on('click', function () {
            var params = $.extend(getQueryParams(), {field: sortObject.field, order: sortObject.type});
            tableIns.reload({where: params, page: {curr: 1}});
        });

        $reset.on('click', function () {
            $searchForm[0].reset();
            sortObject.type = 'null';
            createTimeTo = null;
            createTimeFrom = null;
            tableIns.reload({where: getQueryParams(), page: {curr: 1}, initSort: sortObject});
        });


        $export.on('click', function () {
            var params = $.extend(getQueryParams(), {field: sortObject.field, order: sortObject.type});
            if (!params.year) {
                website.alert.warn('请选择需导出数据的年份');
                return;
            }
            if (!params.planNo) {
                website.alert.warn('请选择需导出数据的期次');
                return;
            }
            website.download(ctx + 'biz/item/export/' + params.year + "-" + params.planNo, null, params.year + "-" + params.planNo + "期结项鉴定统计表.xls");
        });

        /**
         * 初始化表格
         */
        function initTable() {
            tableIns = website.table.init({
                elem: $view.find('table'),
                id: 'itemTable',
                url: ctx + 'biz/item/list',
                where: {topicClassifyCode: "entrust"},
                cols: [[
                    /*{type: 'checkbox'},*/
                    {type: 'numbers', title: '序号'},
                    {field: 'itemCode', title: '立项编号', minWidth: 100},
                    {field: 'name', title: '课题名称', minWidth: 100},
                    {
                        title: '选题名称', minWidth: 100, templet: function (d) {
                            return d.topic.topicName;
                        }
                    },
                    {
                        title: '期次', minWidth: 50, templet: function (d) {
                            return d.topicPlan.period;
                        }
                    },
                    {field: 'topicCode', title: '指南序号', minWidth: 100},
                    {field: 'subjectTypeName', title: '学科分类', templet: '#subjectType', minWidth: 100},
                    {field: 'researchTypeName', title: '研究类型', templet: '#researchType', minWidth: 100},
                    {field: 'leaderName', title: '主持人', minWidth: 100},
                    {field: 'leaderCompany', title: '工作单位', templet: '#leaderCompany', minWidth: 100},
                    {field: 'achievementFormTypeName', title: '成果形式', templet: '#achievementFormType', minWidth: 100},
                    {field: 'statusTypeName', title: '审核状态', templet: '#statusTypeCode', minWidth: 100},
                    {title: '定级', templet: '#levelType', minWidth: 100},
                    {title: '操作', toolbar: '#item-option', minWidth: 150}
                ]]
            });
        }

        /**
         * 获取查询条件
         * @returns {{}}
         */
        function getQueryParams() {
            var params = $searchForm.serializeJson();
            return params;
        }
    })
</script>